<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.orderNo"
        placeholder="订单号"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.carName"
        placeholder="车辆名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.status" placeholder="订单状态" clearable class="filter-item" style="width: 130px">
        <el-option label="进行中" value="进行中" />
        <el-option label="已完成" value="已完成" />
        <el-option label="已取消" value="已取消" />
      </el-select>
      <el-date-picker
        v-model="listQuery.dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="订单号" width="180">
        <template slot-scope="scope">
          {{ scope.row.orderNo }}
        </template>
      </el-table-column>
      <el-table-column label="车辆品牌" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.carName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.plateNumber }}
        </template>
      </el-table-column>
      <el-table-column label="租赁人" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.userName }}
        </template>
      </el-table-column>
      <el-table-column label="租赁时间" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.startDate }} 至 {{ scope.row.endDate }}
        </template>
      </el-table-column>
      <el-table-column label="租赁天数" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.days }}天
        </template>
      </el-table-column>
      <el-table-column label="总金额" width="120" align="center">
        <template slot-scope="scope">
          ¥{{ scope.row.totalAmount }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="200">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row)">
            详情
          </el-button>
          <el-button 
            v-if="row.status === '进行中'" 
            type="success" 
            size="mini" 
            @click="handleComplete(row)"
          >
            完成订单
          </el-button>
          <el-button 
            v-if="row.status === '进行中'" 
            type="danger" 
            size="mini" 
            @click="handleCancel(row)"
          >
            取消订单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'

export default {
  name: 'OrderList',
  components: { Pagination },
  data() {
    return {
      list: [
        {
          orderNo: 'OR202401010001',
          carName: '奥迪A6L',
          plateNumber: '京A12345',
          userName: '张三',
          startDate: '2024-01-01',
          endDate: '2024-01-03',
          days: 3,
          totalAmount: 1500,
          status: '进行中'
        },
        {
          orderNo: 'OR202401010002',
          carName: '丰田凯美瑞',
          plateNumber: '京B67890',
          userName: '李四',
          startDate: '2024-01-02',
          endDate: '2024-01-05',
          days: 4,
          totalAmount: 1200,
          status: '已完成'
        }
      ],
      total: 2,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        orderNo: undefined,
        carName: undefined,
        status: undefined,
        dateRange: []
      }
    }
  },
  methods: {
    getStatusType(status) {
      const statusMap = {
        '进行中': 'warning',
        '已完成': 'success',
        '已取消': 'info'
      }
      return statusMap[status] || 'info'
    },
    getList() {
      this.listLoading = true
      setTimeout(() => {
        // 模拟接口调用
        let filteredList = [...this.list]
        if (this.listQuery.orderNo) {
          filteredList = filteredList.filter(item => item.orderNo.includes(this.listQuery.orderNo))
        }
        if (this.listQuery.carName) {
          filteredList = filteredList.filter(item => item.carName.includes(this.listQuery.carName))
        }
        if (this.listQuery.status) {
          filteredList = filteredList.filter(item => item.status === this.listQuery.status)
        }
        this.list = filteredList
        this.total = filteredList.length
        this.listLoading = false
      }, 500)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$message.info('订单详情功能开发中')
    },
    handleComplete(row) {
      this.$confirm('确认完成该订单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('订单已完成')
        this.getList()
      }).catch(() => {})
    },
    handleCancel(row) {
      this.$confirm('确认取消该订单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('订单已取消')
        this.getList()
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.filter-container {
  padding-bottom: 10px;
}
.filter-item {
  margin-right: 10px;
}
</style>